package pers.hl.module_main.admin.book

import pers.hl.common.base.BaseViewModel
import pers.hl.common.base.SingleLiveEvent
import pers.hl.lib_global.model.bean.Book
import pers.hl.lib_global.model.bean.Comment
import pers.hl.lib_global.model.bean.Reply
import pers.hl.module_main.book_detail.DetailItem

class BookManageViewModel: BaseViewModel<BookManageModel>() {

    fun getAllBooks(): SingleLiveEvent<List<Book>> {
        val allBooksEvent = SingleLiveEvent<List<Book>>()
        launchOnIO {
            val result = mModel.getAllBooks()
            allBooksEvent.postValue(result.data)
        }
        return allBooksEvent
    }

    fun deleteBook(bookId: Int): SingleLiveEvent<String> {
        val deleteBookEvent = SingleLiveEvent<String>()
        launchOnIO {
            val result = mModel.deleteBooks(listOf(bookId))
            deleteBookEvent.postValue(result.msg)
        }
        return deleteBookEvent
    }

    fun fetchComments(bookId: Int): SingleLiveEvent<List<DetailItem.DetailComment>> {
        return createSimpleEvent {
            val result = mModel.fetchComments(bookId)
            val data = result.data
            val list = arrayListOf<DetailItem.DetailComment>()
            data.forEach {
                val detailComment = DetailItem.DetailComment(it)
                list.add(detailComment)
            }
            list
        }
    }

    fun fetchBookInfo(bookId: Int): SingleLiveEvent<Book> {
        return createSimpleEvent {
            val result = mModel.fetchBookInfo(bookId)
            result.data
        }
    }

    fun addComment(bookId: Int, commentInfo: String) : SingleLiveEvent<String> {
        return createSimpleEvent {
            val comment = Comment(cmtBookId = bookId, cmtInfo = commentInfo)
            val result = mModel.addComment(comment)
            result.msg
        }
    }

    fun deleteComment(cmtId: Int): SingleLiveEvent<String> {
        return createSimpleEvent {
            val result = mModel.deleteComment(cmtId)
            result.msg
        }
    }

    fun addReply(cmtId: Int, replyContent: String) : SingleLiveEvent<String> {
        return createSimpleEvent {
            val reply = Reply(rplCmtId = cmtId, rplInfo = replyContent)
            val result = mModel.addReply(reply)
            result.msg
        }
    }

    fun fetchReplyList(cmtId: Int): SingleLiveEvent<List<Reply>> {
        return createSimpleEvent {
            val result = mModel.fetchReplyList(cmtId)
            result.data
        }
    }

    fun collect(bookId: Int): SingleLiveEvent<String> {
        return createSimpleEvent {
            val result = mModel.collect(bookId)
            result.msg
        }
    }

    fun unCollect(bookId: Int): SingleLiveEvent<String> {
        return createSimpleEvent {
            val result = mModel.unCollect(bookId)
            result.msg
        }
    }

    fun borrow(bookId: Int): SingleLiveEvent<String> {
        return createSimpleEvent {
            val result = mModel.borrowBook(bookId)
            result.msg
        }
    }

}
